﻿Shader "Custom/Ground"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _SubColor ("Sub Color", Color) = (1,1,1,1)
        _SubTex ("Sub Albedo (RGB)", 2D) = "white" {}
        _LerpStart("Lerp Start", Float) = 0
        _LerpStop("Lerp Stop", Float) = 1
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _LerpTex("Lerp", 2D) = "white" {}
        _BasePosition("高度", Float) = 10
        _ScaleAndOffset("缩放与偏移", Vector) = (1,1,0,0)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard vertex:vert addshadow

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        float4 _MainTex_ST;
        sampler2D _SubTex;
        float4 _SubTex_ST;
        sampler2D _LerpTex;

        struct Input
        {
            float3 worldPos;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        fixed4 _SubColor;
        half _LerpStart;
        half _LerpStop;
        half _BasePosition;
        half4 _ScaleAndOffset;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void vert(inout appdata_full v, out Input i)
        {
            UNITY_INITIALIZE_OUTPUT(Input, i);

            v.normal = float3(0, 1, 0);

            float ColorNoiseWidth = _ScaleAndOffset.x;
            float ColorNoiseHeight = _ScaleAndOffset.y;
            float ColorNoiseCenterX = _ScaleAndOffset.z;
            float ColorNoiseCenterY = _ScaleAndOffset.w;

            float4 positionWS = mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1.0));
            positionWS.x = positionWS.x - ColorNoiseCenterX + ColorNoiseWidth * 0.5;
            positionWS.z = positionWS.z - ColorNoiseCenterY + ColorNoiseHeight * 0.5;
            positionWS.x /= ColorNoiseWidth;
            positionWS.z /= ColorNoiseHeight;

            fixed4 lerpC = tex2Dlod(_LerpTex, half4(positionWS.xz, 0, 0));

            float height = _BasePosition * lerpC.r;
            v.vertex.y += max(0, height - v.vertex.y);
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            float ColorNoiseWidth = _ScaleAndOffset.x;
            float ColorNoiseHeight = _ScaleAndOffset.y;
            float ColorNoiseCenterX = _ScaleAndOffset.z;
            float ColorNoiseCenterY = _ScaleAndOffset.w;

            float3 positionWS = IN.worldPos;
            positionWS.x = positionWS.x - ColorNoiseCenterX + ColorNoiseWidth * 0.5;
            positionWS.z = positionWS.z - ColorNoiseCenterY + ColorNoiseHeight * 0.5;
            positionWS.x /= ColorNoiseWidth;
            positionWS.z /= ColorNoiseHeight;

            // Albedo comes from a texture tinted by color
            fixed4 mainColor = tex2D(_MainTex, IN.worldPos.xz * _MainTex_ST.xy + _MainTex_ST.zw) * _Color;
            fixed4 subColor = tex2D(_SubTex, IN.worldPos.xz * _SubTex_ST.xy + _SubTex_ST.zw) * _SubColor;

            o.Albedo = lerp(mainColor.rgb, subColor.rgb, saturate(smoothstep(_LerpStart, _LerpStop, IN.worldPos.y)));
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = 1;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
